home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / CyberProcDoggie / Source Code / Bark / main.c < prev   
Encoding:
Text File  |  1995-06-24  |  14.5 KB  |  463 lines  |  [TEXT/SPM ]

  1.  
  2. void InitToolbox()
  3. {
  4.     InitGraf(&qd.thePort);
  5.     FlushEvents(everyEvent, 0);
  6.     MaxApplZone();
  7.     MaxApplZone();
  8. }
  9.  
  10.  
  11. OSErr GetProcessData(StringPtr nameString, char* buffer, Size* bufferLength)
  12. {
  13.     Boolean foundProcess;
  14.     ProcessSerialNumber process;
  15.     ProcessInfoRec infoRec;
  16.     OSErr result;
  17.     FSSpec processFSSpec;
  18.     Str31 tempStr;
  19.     int i;
  20.     long tempNum;
  21.     
  22.     *bufferLength = 0;
  23.     
  24.     process.highLongOfPSN = 0;
  25.     process.lowLongOfPSN = kNoProcess;
  26.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  27.     infoRec.processName = (StringPtr) tempStr;
  28.     infoRec.processAppSpec = &processFSSpec;
  29.     
  30.     while ((result = GetNextProcess(&process)) == noErr)
  31.     {
  32.         result = GetProcessInformation(&process, &infoRec);
  33.         if (result != noErr)
  34.             break;
  35.         
  36.         if (EqualString((ConstStr255Param) infoRec.processName, (ConstStr255Param) nameString, false, true))
  37.             break;
  38.     }
  39.     
  40.     if (result == noErr)
  41.     {
  42.         i = 0;
  43.         
  44.         BlockMove("Process Info for \"", buffer, 24);
  45.         i += 24;
  46.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  47.         i += tempStr[0];
  48.         BlockMove("\"\n", buffer+i, 2);
  49.         i += 2;
  50.         
  51.         BlockMove("\nProcess Serial Number: ", buffer+i, 24);
  52.         i += 24;
  53.         tempNum = (infoRec.processNumber.highLongOfPSN & 0xF0000000) >> 28;
  54.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  55.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x0F000000) >> 24;
  56.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  57.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x00F00000) >> 20;
  58.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  59.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x000F0000) >> 16;
  60.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  61.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x0000F000) >> 12;
  62.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  63.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x00000F00) >> 8;
  64.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  65.         tempNum = (infoRec.processNumber.highLongOfPSN & 0x000000F0) >> 4;
  66.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  67.         tempNum = infoRec.processNumber.highLongOfPSN & 0x0000000F;
  68.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  69.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0xF0000000) >> 28;
  70.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  71.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x0F000000) >> 24;
  72.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  73.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x00F00000) >> 20;
  74.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  75.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x000F0000) >> 16;
  76.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  77.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x0000F000) >> 12;
  78.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  79.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x00000F00) >> 8;
  80.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  81.         tempNum = (infoRec.processNumber.lowLongOfPSN & 0x000000F0) >> 4;
  82.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  83.         tempNum = infoRec.processNumber.lowLongOfPSN & 0x0000000F;
  84.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  85.         
  86.         BlockMove("\nProcess Type:          ", buffer+i, 24);
  87.         i += 24;
  88.         BlockMove(&infoRec.processType, buffer+i, 4);
  89.         i += 4;
  90.         
  91.         BlockMove("\nProcess Creator:       ", buffer+i, 24);
  92.         i += 24;
  93.         BlockMove(&infoRec.processSignature, buffer+i, 4);
  94.         i += 4;
  95.         
  96.         BlockMove("\nProcess Mode:          ", buffer+i, 24);
  97.         i += 24;
  98.         tempNum = (infoRec.processMode & 0xF0000000) >> 28;
  99.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  100.         tempNum = (infoRec.processMode & 0x0F000000) >> 24;
  101.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  102.         tempNum = (infoRec.processMode & 0x00F00000) >> 20;
  103.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  104.         tempNum = (infoRec.processMode & 0x000F0000) >> 16;
  105.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  106.         tempNum = (infoRec.processMode & 0x0000F000) >> 12;
  107.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  108.         tempNum = (infoRec.processMode & 0x00000F00) >> 8;
  109.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  110.         tempNum = (infoRec.processMode & 0x000000F0) >> 4;
  111.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  112.         tempNum = infoRec.processMode & 0x0000000F;
  113.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  114.         
  115.         BlockMove("\nProcess Location:      ", buffer+i, 24);
  116.         i += 24;
  117.         tempNum = (((unsigned long) infoRec.processLocation) & 0xF0000000) >> 28;
  118.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  119.         tempNum = (((unsigned long) infoRec.processLocation) & 0x0F000000) >> 24;
  120.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  121.         tempNum = (((unsigned long) infoRec.processLocation) & 0x00F00000) >> 20;
  122.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  123.         tempNum = (((unsigned long) infoRec.processLocation) & 0x000F0000) >> 16;
  124.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  125.         tempNum = (((unsigned long) infoRec.processLocation) & 0x0000F000) >> 12;
  126.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  127.         tempNum = (((unsigned long) infoRec.processLocation) & 0x00000F00) >> 8;
  128.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  129.         tempNum = (((unsigned long) infoRec.processLocation) & 0x000000F0) >> 4;
  130.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  131.         tempNum = ((unsigned long) infoRec.processLocation) & 0x0000000F;
  132.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  133.         
  134.         BlockMove("\nProcess Size:          ", buffer+i, 24);
  135.         i += 24;
  136.         NumToString(infoRec.processSize, tempStr);
  137.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  138.         i += tempStr[0];
  139.         
  140.         BlockMove("\nProcess Free Mem:      ", buffer+i, 24);
  141.         i += 24;
  142.         NumToString(infoRec.processFreeMem, tempStr);
  143.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  144.         i += tempStr[0];
  145.         
  146.         BlockMove("\nProcess Launched By:   ", buffer+i, 24);
  147.         i += 24;
  148.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0xF0000000) >> 28;
  149.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  150.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x0F000000) >> 24;
  151.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  152.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x00F00000) >> 20;
  153.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  154.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x000F0000) >> 16;
  155.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  156.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x0000F000) >> 12;
  157.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  158.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x00000F00) >> 8;
  159.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  160.         tempNum = (infoRec.processLauncher.highLongOfPSN & 0x000000F0) >> 4;
  161.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  162.         tempNum = infoRec.processLauncher.highLongOfPSN & 0x0000000F;
  163.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  164.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0xF0000000) >> 28;
  165.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  166.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x0F000000) >> 24;
  167.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  168.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x00F00000) >> 20;
  169.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  170.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x000F0000) >> 16;
  171.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  172.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x0000F000) >> 12;
  173.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  174.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x00000F00) >> 8;
  175.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  176.         tempNum = (infoRec.processLauncher.lowLongOfPSN & 0x000000F0) >> 4;
  177.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  178.         tempNum = infoRec.processLauncher.lowLongOfPSN & 0x0000000F;
  179.         *(buffer+(i++)) = (tempNum < 10) ? '0' + tempNum : 'A' + tempNum - 10;
  180.         
  181.         /*
  182.         BlockMove("\nProcess Launch Time:   ", buffer+i, 24);
  183.         i += 24;
  184.         DateString((long) infoRec.processLaunchDate, abbrevDate, (StringPtr) tempStr, nil);
  185.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  186.         i += tempStr[0];
  187.         BlockMove("  ", buffer+i, 2);
  188.         i += 2;
  189.         TimeString((long) infoRec.processLaunchDate, true, (StringPtr) tempStr, nil);
  190.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  191.         i += tempStr[0];
  192.         */
  193.         
  194.         BlockMove("\nProcess Active Time:   ", buffer+i, 24);
  195.         i += 24;
  196.         NumToString(infoRec.processActiveTime, tempStr);
  197.         BlockMove(tempStr+1, buffer+i, tempStr[0]);
  198.         i += tempStr[0];
  199.         BlockMove(" ticks\n", buffer+i, 24);
  200.         
  201.         *bufferLength = i;
  202.     }
  203.     
  204.     return result;
  205. }
  206.  
  207.  
  208. /*
  209. OSErr DoneReqParams(AppleEvent* appleEvent)
  210. {
  211.     DescType typeCode;
  212.     Size actualSize;
  213.     OSErr err;
  214.     
  215.     err = AEGetAttributePtr(appleEvent, keyMissedKeywordAttr,
  216.             typeWildCard, &typeCode, NULL, 0, &actualSize);
  217.     if (err == errAEDescNotFound)
  218.         return noErr;
  219.     else if (err == noErr)
  220.         return errAEEventNotHandled;
  221.     else
  222.         return err;
  223. }
  224. */
  225.  
  226. Boolean gquit;
  227.  
  228. OSErr HandleAEQuit(AppleEvent* quitAppleEvent, AppleEvent* reply,
  229.             long handlerRefCon)
  230. {
  231.     OSErr err;
  232.     
  233.     /*
  234.     err = DoneReqParams(quitAppleEvent);
  235.     if (err != noErr)
  236.         return err;    // set quit flag anyway?
  237.     */
  238.     gquit = true;
  239.     return noErr;
  240. }
  241.  
  242.  
  243.  
  244. void main()
  245. {
  246.     OSErr err;
  247.     AEEventHandlerUPP upp;
  248.     EventRecord event;
  249.     Boolean isOpen = false;
  250.     enum {forInform, forRead, forWrite} waitingState;
  251.     Size bufferLen;
  252.     PPCOpenPBRec openpb;
  253.     PPCInformPBRec informpb;
  254.     PPCClosePBRec closepb;
  255.     PPCAcceptPBRec acceptpb;
  256.     PPCReadPBRec readpb;
  257.     PPCWritePBRec writepb;
  258.     PPCPortRec portName;
  259.     char buffer[1024];
  260.     
  261.     InitToolbox();
  262.     
  263.     upp = NewAEEventHandlerProc(HandleAEQuit);
  264.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  265.         upp, 0L, false);
  266.     if (err != noErr)
  267.     {
  268.         DebugStr("\pAEInstallEventHandler failed");
  269.         return;
  270.     }
  271.     gquit = false;
  272.     
  273.     err = PPCInit();
  274.     if (err <noErr)
  275.     {
  276.         DebugStr("\pPPCInit failed");
  277.         return;
  278.     }
  279.     
  280.     portName.nameScript = smSystemScript;
  281.     BlockMove(portName.name, "\pBark", 5);
  282.     portName.portKindSelector = ppcByCreatorAndType;
  283.     portName.u.port.creator = 'BæRK';
  284.     portName.u.port.type = 'BæRK';
  285.     openpb.ioCompletion = NULL;
  286.     openpb.serviceType = ppcServiceRealTime;
  287.     openpb.resFlag = 0;
  288.     openpb.portName = &portName;
  289.     openpb.locationName = NULL;
  290.     openpb.networkVisible = true;
  291.     err = PPCOpen(&openpb, false);
  292.     if (err < noErr)
  293.     {
  294.         DebugStr("\pPPCOpen failed");
  295.         return;
  296.     }
  297.     isOpen = true;
  298.     
  299.     informpb.ioCompletion = NULL;
  300.     informpb.portRefNum = openpb.portRefNum;
  301.     informpb.autoAccept = true;
  302.     informpb.portName = 0;
  303.     informpb.locationName = NULL;
  304.     informpb.userName = NULL;
  305.     err = PPCInform(&informpb, true);    // async!
  306.     if (err < noErr)
  307.     {
  308.         DebugStr("\pPPCInform failed");
  309.         closepb.ioCompletion = NULL;
  310.         closepb.portRefNum = openpb.portRefNum;
  311.         PPCClose(&closepb, false);
  312.         return;
  313.     }
  314.     waitingState = forInform;
  315.     
  316.     // loop while polling :-)
  317.     while (!gquit)
  318.     {
  319.         if (WaitNextEvent(highLevelEventMask, &event, 60, NULL))
  320.         {
  321.             err = AEProcessAppleEvent(&event);
  322.             if (err != noErr)
  323.             {
  324.                 DebugStr("\pAEProcessAppleEvent failed");
  325.                 break;
  326.             }
  327.         }
  328.         
  329.         if (waitingState == forInform)
  330.         {
  331.             if (informpb.ioResult != 1)
  332.             {
  333.                 if (informpb.ioResult != noErr)
  334.                 {
  335.                     DebugStr("\pPPCInform failed on completion");
  336.                     break;
  337.                 }
  338.                 
  339.                 acceptpb.ioCompletion = NULL;
  340.                 acceptpb.sessRefNum = informpb.sessRefNum;
  341.                 err = PPCAccept(&acceptpb, false);
  342.                 if (err != noErr)
  343.                 {
  344.                     DebugStr("\pPPCAccept failed");
  345.                     break;
  346.                 }
  347.                 
  348.                 readpb.ioCompletion = NULL;
  349.                 readpb.sessRefNum = informpb.sessRefNum;
  350.                 readpb.bufferLength = 1024;
  351.                 readpb.bufferPtr = buffer;
  352.                 err = PPCRead(&readpb, true);    // async!
  353.                 if (err != noErr)
  354.                 {
  355.                     DebugStr("\pPPCRead failed");
  356.                     break;
  357.                 }
  358.                 
  359.                 waitingState = forRead;
  360.             }
  361.         }
  362.         else if (waitingState == forRead)
  363.         {
  364.             if (readpb.ioResult != 1)
  365.             {
  366.                 if (readpb.ioResult != noErr)
  367.                 {
  368.                     DebugStr("\pPPCRead failed on completion");
  369.                     break;
  370.                 }
  371.                 
  372.                 // ok! get data and send it
  373.                 buffer[readpb.actualLength] = 0;
  374.                 c2pstr(buffer);
  375.                 DebugStr((StringPtr) buffer);
  376.                 err = GetProcessData((StringPtr) buffer, buffer, &bufferLen);
  377.                 if (err != noErr)
  378.                 {
  379.                     DebugStr("\pGetProcessData failed");
  380.                     break;
  381.                 }
  382.                 
  383.                 writepb.ioCompletion = NULL;
  384.                 writepb.sessRefNum = informpb.sessRefNum;
  385.                 writepb.bufferLength = bufferLen;
  386.                 writepb.bufferPtr = buffer;
  387.                 writepb.more = false;
  388.                 err = PPCWrite(&writepb, true);
  389.                 if (err != noErr)
  390.                 {
  391.                     DebugStr("\pPPCWrite failed");
  392.                     break;
  393.                 }
  394.                 
  395.                 waitingState = forWrite;
  396.             }
  397.         }
  398.         else if (waitingState == forWrite)
  399.         {
  400.             if (writepb.ioResult != 1)
  401.             {
  402.                 if (writepb.ioResult != noErr)
  403.                 {
  404.                     DebugStr("\pPPCWrite failed on completion");
  405.                     break;
  406.                 }
  407.                 
  408.                 // close, open & inform again
  409.                 isOpen = false;
  410.                 closepb.ioCompletion = NULL;
  411.                 closepb.portRefNum = openpb.portRefNum;
  412.                 err = PPCClose(&closepb, false);
  413.                 if (err != noErr)
  414.                 {
  415.                     DebugStr("\pPPCClose failed");
  416.                     break;
  417.                 }
  418.                 
  419.                 portName.nameScript = smSystemScript;
  420.                 BlockMove(portName.name, "\pBark", 5);
  421.                 portName.portKindSelector = ppcByCreatorAndType;
  422.                 portName.u.port.creator = 'BæRK';
  423.                 portName.u.port.type = 'BæRK';
  424.                 openpb.ioCompletion = NULL;
  425.                 openpb.serviceType = ppcServiceRealTime;
  426.                 openpb.resFlag = 0;
  427.                 openpb.portName = &portName;
  428.                 openpb.locationName = NULL;
  429.                 openpb.networkVisible = true;
  430.                 err = PPCOpen(&openpb, false);
  431.                 if (err < noErr)
  432.                 {
  433.                     DebugStr("\pPPCOpen failed");
  434.                     break;
  435.                 }
  436.                 isOpen = true;
  437.                 
  438.                 informpb.ioCompletion = NULL;
  439.                 informpb.portRefNum = openpb.portRefNum;
  440.                 informpb.autoAccept = true;
  441.                 informpb.portName = 0;
  442.                 informpb.locationName = NULL;
  443.                 informpb.userName = NULL;
  444.                 err = PPCInform(&informpb, true);    // async!
  445.                 if (err < noErr)
  446.                 {
  447.                     DebugStr("\pPPCInform failed");
  448.                     break;
  449.                 }
  450.                 waitingState = forInform;
  451.             }
  452.         }
  453.     }
  454.     
  455.     if (isOpen)
  456.     {
  457.         closepb.ioCompletion = NULL;
  458.         closepb.portRefNum = openpb.portRefNum;
  459.         PPCClose(&closepb, false);
  460.     }
  461. }
  462.  
  463.